package cn.com.zpark.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @PackageName: cn.com.zpark.cookie
 * @ClassName: CookieDemo03
 * @Description:
 * @author: RZS
 * @date: 2021/11/15  15:00
 */
@WebServlet("/cd3.do")
public class CookieDemo03 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 将cookie设置到浏览器，并且设置其存活时间
         */
        // 创建cookie
        Cookie cookie1 = new Cookie("data1",
                URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "utf-8"));
        Cookie cookie2 = new Cookie("data2", "哈哈哈");
        //cookie2.setValue("zhangsan");
        cookie2.setMaxAge(60*3);

        // 将cookie设置到浏览器
        response.addCookie(cookie1);
        response.addCookie(cookie2);


    }
}
